﻿@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<table border="none">
<tr><td><b>DESCRIPTION</b></td><td align="center"><b>XML</b></td><td align="center"><b>JSON</b></td></tr>
<tr>
    <td>To retrieve the list of products press one of these buttons:</td>
    <td><button id="retrieveProductsXml" title="Retrive Products as XML" >Retrive Products as XML</button></td>
    <td><button id="retrieveProductsJson" title="Retrive Products as JSON" >Retrive Products as JSON</button></td>
</tr>
<tr>
    <td>To retrieve an individual product (id=0) press one of these buttons:</td>
    <td><button id="retrieveProduct0Xml" title="Retrive Product 0 as XML" >Retrive Product 0 as XML</button></td>
    <td><button id="retrieveProduct0Json" title="Retrive Product 0 as JSON" >Retrive Product 0 as JSON</button></td>
</tr>
<tr>
    <td>To PUT a new version of a product (id=0) back, press one of these buttons:</td>
    <td><button id="putProduct0Xml" title="Put Product 0 as XML" >Put Product 0 as XML</button></td>
    <td><button id="putProduct0Json" title="Putt Product 0 as JSON" >Put Product 0 as JSON</button></td>
</tr>
<tr>
    <td>To pre-populate a new product ready for POSTing back, press one of these buttons:</td>
    <td><button id="prepareProductXml" title="Prepare New Product as XML" >Prepare New Product as XML</button></td>
    <td><button id="prepareProductJson" title="Prepare New Product as JSON" >Prepare New Product as JSON</button></td>
</tr>
<tr>
    <td>To POST a new product back, press one of these buttons:</td>
    <td><button id="postProductXml" title="Post Product as XML" >Post Product as XML</button></td>
    <td><button id="postProductJson" title="Post Product as JSON" >Post Product as JSON</button></td>
</tr>
</table>

<p><textarea id="results" cols="100" rows="20" title="Contents of the message"></textarea></p>

<script type="text/javascript">
<!--

    // Retrieve current products

    function retrieveProductsXml() {
        retrieveProducts("application/xml");
    }

    function retrieveProductsJson() {
        retrieveProducts("application/json");
    }

    function retrieveProducts(format) {

        jQuery.ajax({
            type: "GET",
            url: "services/products",
            beforeSend: function (xhr) { xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); xhr.setRequestHeader("Accept", format); },
            complete: onRequestComplete,
            data: "",
            processData: false
        });
    }

    // Retrieve product 0

    function retrieveProduct0Xml() {
        retrieveProduct0("application/xml");
    }

    function retrieveProduct0Json() {
        retrieveProduct0("application/json");
    }

    function retrieveProduct0(format) {

        jQuery.ajax({
            type: "GET",
            url: "services/products/0",
            beforeSend: function (xhr) { xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); xhr.setRequestHeader("Accept", format); },
            complete: onRequestComplete,
            data: "",
            processData: false
        });
    }

    function onRequestComplete(xhr) {
        $('#results').val(xhr.responseText);
    }

    // Put products

    function putProduct0Xml() {
        putProduct0("application/xml");
    }

    function putProduct0Json() {
        putProduct0("application/json");
    }

    function putProduct0(format) {

        jQuery.ajax({
            type: "PUT",
            url: "services/products/0",
            beforeSend: function (xhr) { xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); xhr.setRequestHeader("Accept", format); },
            complete: onPutComplete,
            contentType: format,
            data: $('#results').val(),
            processData: false
        });
    }

    function onPutComplete(xhr) {
        $('#results').val(xhr.responseText);
    }

    // Prepare new products

    function prepareProductXml() {
        $('#results').val("<Product>\r\n  <Name>New</Name>\r\n  <UnitPrice>4.3</UnitPrice>\r\n  <UnitsInStock>5</UnitsInStock>\r\n</Product>");
    }

    function prepareProductJson() {
        $('#results').val("{\r\n  \"Name\":\"New\",\r\n  \"UnitPrice\":4.3,\r\n  \"UnitsInStock\":5\r\n}");
    }

    // Create new products

    function postProductXml() {
        postProducts("application/xml");
    }

    function postProductJson() {
        postProducts("application/json");
    }

    function postProducts(format) {

        jQuery.ajax({
            type: "POST",
            url: "services/products",
            beforeSend: function (xhr) { xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); xhr.setRequestHeader("Accept", format); },
            complete: onPostComplete,
            contentType: format,
            data: $('#results').val(),
            processData: false
        });
    }

    function onPostComplete(xhr) {
        $('#results').val(xhr.responseText);
    }


    $('#retrieveProductsXml').click(retrieveProductsXml);
    $('#retrieveProductsJson').click(retrieveProductsJson);
    $('#retrieveProduct0Xml').click(retrieveProduct0Xml);
    $('#retrieveProduct0Json').click(retrieveProduct0Json);
    $('#putProduct0Xml').click(putProduct0Xml);
    $('#putProduct0Json').click(putProduct0Json);
    $('#prepareProductXml').click(prepareProductXml);
    $('#prepareProductJson').click(prepareProductJson);
    $('#postProductXml').click(postProductXml);
    $('#postProductJson').click(postProductJson);
// -->            
</script>
